#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include <QPushButton>
#include <QDebug>
#include <QMenu>
#include <QAction>
#include <QDialog>
#include <QColorDialog>
#include <QMessageBox>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //设置大小
    //this->resize();
    this->setFixedSize(500,380);

    this->setWindowTitle("弹窗小Demo");

//    QIcon * icon = new QIcon(":/new/prefix1/Image/头像.jpg");
//    this->setWindowIcon(*icon);

    this->setWindowIcon(QIcon(":/new/prefix1/Image/头像.jpg"));

    const int MOVE = 120;
    const int offsetX = 20;
    const int offsetY = 20;

    QPushButton * btn1 = new QPushButton("非模态",this);
    btn1->setGeometry(offsetX + 0 * MOVE, offsetY + 0 * MOVE, 100, 100);
//    btn1->move(20,20); //绝对值
    connect(btn1, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮1" ;

        QDialog * dlg2 = new QDialog(this);
        dlg2->resize(200,100);
        dlg2->show();
        //这句很重要，关掉的时候就释放空间
        dlg2->setAttribute(Qt::WA_DeleteOnClose);
    });

    QPushButton * btn2 = new QPushButton("模态",this);
    btn2->setGeometry(offsetX + 1 * MOVE, offsetY + 0 * MOVE, 100, 100);
    btn2->setFlat(true);
    connect(btn2, &QPushButton::clicked, this, [=](){
        //由于是模态，可以使用栈区变量
        qDebug() << "按下了按钮2" ;
        QDialog dlg(this);
        dlg.resize(200,100);
        dlg.exec();
    });

    QPushButton * btn3 = new QPushButton("模态3",this);
    btn3->setGeometry(offsetX + 2 * MOVE, offsetY + 0 * MOVE, 100, 100);
//    btn3->setFlat(true);
    btn3->setCheckable(true);
    connect(btn3, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮3" ;
        QDialog * dlg3 = new QDialog(this);
        //设置模态
//        dlg3->setModal(true);
        //或者同效果
        dlg3->setAttribute(Qt::WA_ShowModal,true);

        dlg3->resize(200,100);
        dlg3->show();
        dlg3->setAttribute(Qt::WA_DeleteOnClose,true);
    });

    QPushButton * btn4 = new QPushButton("测试按钮4",this);
    btn4->setGeometry(offsetX + 3 * MOVE, offsetY + 0 * MOVE, 100, 100);
    btn4->setFlat(true);
    btn4->setCheckable(true);
    connect(btn4, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮4" ;
        QDialog * dlg4 = new QDialog(this);
        dlg4->setAttribute(Qt::WA_DeleteOnClose,true);
        dlg4->resize(200,200);
        dlg4->show();
    });

    QPushButton * btn5 = new QPushButton("按钮5",this);
    btn5->setGeometry(offsetX + 0 * MOVE, offsetY + 1 * MOVE, 100, 100);
    QMenu * menu = new QMenu(btn5);
    QAction * halo = menu->addAction("哈喽");
    btn5->setMenu(menu);
    connect(btn5, &QPushButton::toggled, this, [=](){
        qDebug() << "按下了按钮5" ;
    });
    connect(halo, &QAction::triggered, this, [=](){
        qDebug() << "按下了按钮5，哈喽" ;
    });

    QPushButton * btn6 = new QPushButton(this);
    btn6->setGeometry(offsetX + 1 * MOVE, offsetY + 1 * MOVE, 100, 100);
    QIcon(":/new/prefix1/Image/头像.jpg");
    btn6->setIcon(QIcon(":/new/prefix1/Image/头像.jpg"));
    connect(btn6, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮6" ;
    });

    QPushButton * btn7 = new QPushButton("颜色框",this);
    btn7->setGeometry(offsetX + 2 * MOVE, offsetY + 1 * MOVE, 100, 100);

    connect(btn7, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮7" ;
        //这是调用了颜色控件的一个静态函数
        QColor color = QColorDialog::getColor(Qt::white, this);
        QPalette pal = btn7->palette();
        pal.setColor(QPalette::Button,color);
        btn7->setPalette(pal);
        btn7->setAutoFillBackground(true);
        btn7->setFlat(true);
    });

    QPushButton * btn8 = new QPushButton("颜色框2",this);
    btn8->setGeometry(offsetX + 3 * MOVE, offsetY + 1 * MOVE, 100, 100);
    connect(btn8, &QPushButton::clicked, this, [=](){
        QColor color = QColorDialog::getColor(Qt::white, this);
        QPalette pal = btn8->palette();
        pal.setColor(QPalette::Button,color);
        btn8->setPalette(pal);
        btn8->setAutoFillBackground(true);
//        btn8->setFlat(true);
        qDebug() << "按下了按钮8" << color;
    });

    QPushButton * btn9 = new QPushButton("错误",this);
    btn9->setGeometry(offsetX + 0 * MOVE, offsetY + 2 * MOVE, 100, 100);
    connect(btn9, &QPushButton::clicked, this, [=](){
        //有四个静态函数
        QMessageBox::critical(this,"标题","显示错误");
        QMessageBox::information(this,"标题","显示信息");
        QMessageBox::question(this,"标题","显示问题");
        QMessageBox::warning(this,"标题","显示警告");
        qDebug() << "按下了按钮9" ;
    });

    QPushButton * btn10 = new QPushButton("文件",this);
    btn10->setGeometry(offsetX + 1 * MOVE, offsetY + 2 * MOVE, 100, 100);
    connect(btn10, &QPushButton::clicked, this, [=](){
        QString file = QFileDialog::getOpenFileName(this,"打开文件","E:\workspace_html");
        qDebug() << "按下了按钮10" ;
        qDebug() << file ;
    });

    QPushButton * btn11 = new QPushButton("字体",this);
    btn11->setGeometry(offsetX + 2 * MOVE, offsetY + 2 * MOVE, 100, 100);
    connect(btn11, &QPushButton::clicked, this, [=](){
        bool flag;
        QFont font = QFontDialog::getFont(&flag,QFont("新宋体",36));
        //搜嘎，其实刚才看到flag是引用就应该知道了，flag是判断字体是否加载成功的标志
        //接下来应该是 if (flag){}    else {}
        qDebug() << "按下了按钮11" ;
        qDebug() << font;
    });

    QPushButton * btn12 = new QPushButton("最终",this);
    btn12->setGeometry(offsetX + 3 * MOVE, offsetY + 2 * MOVE, 100, 100);
    connect(btn12, &QPushButton::clicked, this, [=](){
        qDebug() << "按下了按钮12" ;
        QString str = QInputDialog::getText(this,"标题","");
        qDebug() << str;
        int num = QInputDialog::getInt(this,"","");
        qDebug() << num;
        QStringList list ;
        list << "帅" << "很帅" << "相当帅" << "超级帅";
        QString str2 = QInputDialog::getItem(this,"彪哥帅不","说实话",list);
        qDebug() << str2;
    });


}

MainWindow::~MainWindow()
{
    delete ui;
}
